﻿/// <reference path="../jquery-1.7.2-vsdoc.js" />

$(document).ready(function () {
    questionIndex = $('[id^="question"]').length;

    $("form:first").submit(saveQuestionnaire);
});

function saveQuestionnaire() {
    var result = new Object();

    var i = 0;
    var j = 0;

    result.ID = $('#ID').val();
    result.Name = $('#Name').val();
    result.Description = $('#Description').val();
    result.EvaluatorClass = $('#EvaluatorClass').val();

    result.Questions = new Array();
    $('#qList').children('li').each(function () {
        result.Questions[i] = new Object();
        result.Questions[i].Position = i;
        result.Questions[i].Text = $(this).find('#qText').val();
        result.Questions[i].HasFreeAnswer = $(this).find('[id^="freeText"]').is(':checked');
        result.Questions[i].HasMultipleChoice = $(this).find('[id^="multipleChoise"]').is(':checked');
        result.Questions[i].Answers = new Array();
        j = 0;
        $(this).find('[id^="aList"]').find('li').each(function myfunction() {
            result.Questions[i].Answers[j] = new Object();
            result.Questions[i].Answers[j].Position = j;
            result.Questions[i].Answers[j].Text = $(this).find('#qAnswer').val();
            result.Questions[i].Answers[j].Value = $(this).find('#qAValue').val();
            j++;
        });
        i++;
    });

    $('#jsonResult').val(JSON.stringify(result));
}

function freeTextChanged(checked, index) {
    if (checked) {
        $('#aList' + index).hide();
        $('#multipleChoise' + index).attr('checked', false)
        $('#multipleChoise' + index).attr('disabled', true);
        $('#question' + index).find('#btnNewAnswer').css('display', 'none');
    }
    else {
        $('#aList' + index).show();
        $('#multipleChoise' + index).removeAttr('disabled');
        $('#question' + index).find('#btnNewAnswer').removeAttr('style');
    }
}

function deleteQuestion(questionId) {
    $('#' + questionId).remove();

    var count = $('[id^="question"]').length;
    if (count == 1) {
        $('[id^="question"]').children('img').css('display', 'none');
    }
}

function deleteAnswer(aListId, answerId) {
    $('#' + aListId).find('#' + answerId).remove();
    var listLength = $('#' + aListId).find('li').length;
    if (listLength == 1) {
        $('#' + aListId).find('img').css('display', 'none');
    }
}

function newAnswer(answerIndex) {

    var listLength = $('#aList' + answerIndex).find('li').length;
    if (listLength == 1) {
        $('#aList' + answerIndex).find('img').removeAttr('style');
    }

    var lastId = $('#aList' + answerIndex).find('li').last().attr('id');
    var index = lastId.replace('answer','');
    index++;
    var elem = $('#clone_answer').first().clone().attr('id', 'answer' + index);
    $(elem).children('img').attr('onclick', 'deleteAnswer("aList' + answerIndex + '","' + 'answer' + index + '")');
    $(elem).children('img').removeAttr('style');
    $('#aList' + answerIndex).append(elem);
}

function newQuestion() {
    var elem = $('#clone_question').first().clone();
    var crtQuestion = questionIndex++;

    var count = $('[id^="question"]').length;
    if (count == 1) {
        $('[id^="question"]').children('img').removeAttr('style');
    }

    $(elem).attr('id', 'question' + crtQuestion);
    $(elem).children('img').attr('onclick', 'deleteQuestion("question' + crtQuestion + '");');

    $(elem).find('#clone_answer').attr('id', 'answer0');
    $(elem).find('#clone_answer').find('img').attr('onclick', 'deleteAnswer("aList' + crtQuestion + '","answer0")');
    $(elem).children('img').attr('onclick', 'deleteQuestion("question' + crtQuestion + '");');

    $(elem).find('#clone_freeText').attr('onclick', 'freeTextChanged(this.checked,' + crtQuestion + ')');
    $(elem).find('#clone_freeText').attr('id', 'freeText' + crtQuestion);

    $(elem).find('#clone_multipleChoise').attr('id', 'multipleChoise' + crtQuestion);

    $(elem).find('#clone_aList').attr('id', 'aList' + crtQuestion);
    $(elem).find('#btnNewAnswer').attr('onclick', 'newAnswer(' + crtQuestion + ');');

    $('#qList').append(elem);
}